package com.kgc.dao;

import com.kgc.entity.User;
import com.kgc.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao {
    public User selectLogin(String phone) {
        try {
            String sql = "select * from user where phone=?";
            ResultSet rs = super.getExecuteQuery(sql, phone);
            while (rs.next()) {
                return new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
                        rs.getString(5), rs.getInt(6), rs.getInt(7), rs.getString(8));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }

    //注册新用户
    public int addUser(String username, String password, String phone, String email, int sex, String salt) {
        try {
            String sql = "INSERT INTO user value(null,?,?,?,?,?,1,?)";
            return super.getExecuteUpdate(sql, username, password, phone, email, sex, salt);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }


    /**
     * 业主总数
     *
     * @return
     */
    public int totalUser(String username) {
        try {
            String sql = "select count(*) from user";
            if (StringUtils.isNotEmpty(username)) {
                sql += " where username like '%" + username + "%'";
            }
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }


    /**
     * 用户列表
     *
     * @param pageUtils
     * @param username
     * @return
     */
    public List<User> selectPage(PageUtils pageUtils, String username) {
        List<User> list = new ArrayList<>();
        try {
            String sql = "select * from User ";
            if (StringUtils.isNotEmpty(username)) {
                sql += " where username like '%" + username + "%'";
            }
            sql += " limit ?,?";
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getInt(7), rs.getString(8)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return list;
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    public int deleteUser(String id) {
        String sql = "delete from `User` where id = ?";
        return super.getExecuteUpdate(sql, id);
    }


    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    public int updateUser(User user) {
        String sql = "UPDATE `user` SET `username` = ?, `phone` = ?, `sex` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, user.getUsername(), user.getPhone(), user.getSex(), user.getId());
    }

}
